<?php
/**
 * 商品 业务层
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-09-05
 * Time: 17:58
 */

namespace App\Services\Manager;


use App\Models\Brand;
use App\Models\Expressfee;
use App\Models\ExpressfeeCity;
use App\Models\Goods;
use App\Models\GoodsStock;
use App\Models\GoodStock;
use App\Models\GoodStockBatch;
use App\Models\GoodStockBatchLog;
use App\Models\InventoryLog;
use App\Models\Kind;
use App\Models\Supplier;
use App\Models\UserManager;

class GoodsService
{
    public function verify($good,$id,$integral = 0)
    {
        $supplier = Supplier::where('status', 1)->where('id',$good['supplier_id'])->first();
        if(!$supplier){
            return -1;
        }
        //缺个重量模板是否重量为0,和是否选择运费模板,和运费模板是否存在并且全部规则都已设计
        $isAgravic = 0;
        $expressfee = Expressfee::where('id',$good['expressfee_id'])->first();
        if(!$expressfee){
            return -2;
        }
        $zhongliangExpressfee = ExpressfeeCity::where('expressfeeid',$expressfee['id'])->where('unit',0)->count();
        if($zhongliangExpressfee){
            $unit = 0;
        }else{
            $unit = 1;
        }

        $nullPrice = 0;
        $littleGoodsStock = null;
        if($integral){
            $goodsStockList = GoodsStock::where('goods_integral_id', $id)->get()->toArray();
            unset($value);
            foreach ($goodsStockList as $value) {
                if ($value['use_integral'] <= 0) {
                    $nullPrice = 1;
                }
                if ($value['cost_price'] <= 0 && $value['pure_cost_price'] <= 0) {
                    return -6;
                }
                if(!isset($value['image']) || empty($value['image'])){
                    return -5;
                }
                if($unit == 0){
                    if($value['zhongliang'] <= 0){
                        return -2;
                    }
                }
                $littleGoodsStock = $value;
            }
        }else{
            $goodsStockList = GoodsStock::where('goods_id', $id)->get()->toArray();
            unset($value);
            foreach ($goodsStockList as $value) {
                if ($value['price'] <= 0 || $value['vip_price'] <= 0) {
                    $nullPrice = 1;
                }
                if ($value['cost_price'] <= 0 && $value['pure_cost_price'] <= 0) {
                    return -6;
                }
                if(!isset($value['image']) || empty($value['image'])){
                    return -5;
                }
                if($unit == 0){
                    if($value['zhongliang'] <= 0){
                        return -2;
                    }
                }
                $littleGoodsStock = $value;
            }
        }
        if ($isAgravic) {
            return -2;
        }
        if (!isset($littleGoodsStock)) {
            return -3;
        }
        if ($nullPrice) {
            return -4;
        }
        return 1;
    }
}